package com.itheima.dao.impl;

import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao {
    private JdbcTemplate jdbcTemplate;

    public Account findAccountByName(String name) {
        String sql = "select * from account where name = ?";
        Account account = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), name);
        return account;
    }

    public int transferTo(String outName, float money) {
        String sql = "update account set money = money - ? where name = ?";
        int count = jdbcTemplate.update(sql, money, outName);
        return count;
    }

    public int transferIn(String inName, float money) {
        String sql = "update account set money = money + ? where name = ?";
        int count = jdbcTemplate.update(sql, money,inName);
        return count;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate=jdbcTemplate;
    }
}
